package capstoneproject.dudiachi.Main;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import capstoneproject.dudiachi.Controller.FestivalManager;

public class FestivalCalendarActivity extends BaseActivity implements
		OnClickListener, OnGesturePerformedListener {

	private Calendar calendar;
	private int date, month, year;
	private ImageView prevMonth;
	private TextView currentMonth;
	private ImageView nextMonth;
	private GridView calendarView;
	private GridView weekdays;
	private WeekdayGridCellAdapter wdAdapter;
	private FestivalCalendarGridCellAdapter calendarAdapter;
	private List<String> listWD;
	private FestivalManager festivalManager;
	GestureOverlayView mOverlay;
	GestureLibrary gestureLib;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		BaseActivity.POS = 1;
		setContentView(R.layout.activity_festival_calendar);

		calendar = Calendar.getInstance(Locale.getDefault());
		date = calendar.get(Calendar.DAY_OF_MONTH);
		month = calendar.get(Calendar.MONTH) + 1;
		year = calendar.get(Calendar.YEAR);

		prevMonth = (ImageView) findViewById(R.id.prevMonth);
		prevMonth.setOnClickListener(this);

		String monthAsString = "";
		monthAsString = convertMonth(month);
		currentMonth = (TextView) findViewById(R.id.currentMonth);
		currentMonth.setText("" + monthAsString + ", " + year);
		currentMonth.setOnClickListener(this);

		nextMonth = (ImageView) findViewById(R.id.nextMonth);
		nextMonth.setOnClickListener(this);

		// week days adapter
		listWD = new ArrayList<String>();
		listWD.add("CN");
		listWD.add("Hai");
		listWD.add("Ba");
		listWD.add("Tư");
		listWD.add("Năm");
		listWD.add("Sáu");
		listWD.add("Bảy");
		weekdays = (GridView) findViewById(R.id.weekdays);
		wdAdapter = new WeekdayGridCellAdapter(getApplicationContext(), listWD);
		weekdays.setAdapter(wdAdapter);

		// calendar adapter

		festivalManager = new FestivalManager(this);
		calendarView = (GridView) findViewById(R.id.calendar);
		calendarAdapter = new FestivalCalendarGridCellAdapter(getApplicationContext(),
				R.id.calendar_day_gridcell, date, month, year, festivalManager);
		calendarAdapter.notifyDataSetChanged();
		calendarView.setAdapter(calendarAdapter);

		gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
		mOverlay = (GestureOverlayView) findViewById(R.id.gestures);
		mOverlay.addOnGesturePerformedListener(this);
		if (!gestureLib.load()) {
			Toast.makeText(this, "Couldn't load the gesture library",
					Toast.LENGTH_SHORT).show();
		}
	}

	private void setGridCellAdapterToDate(int date, int month, int year) {

		String monthAsString = "";
		monthAsString = convertMonth(month);
		calendarAdapter = new FestivalCalendarGridCellAdapter(getApplicationContext(),
				R.id.calendar_day_gridcell, date, month, year,
				this.festivalManager);
		calendar.set(year, month - 1, calendar.get(Calendar.DAY_OF_MONTH));
		currentMonth.setText("" + monthAsString + ", " + year);
		calendarAdapter.notifyDataSetChanged();
		calendarView.setAdapter(calendarAdapter);

	}

	@Override
	public void onClick(View v) {
		if (v == prevMonth) {
			if (month <= 1) {
				month = 12;
				year--;
			} else {
				month--;
			}
			setGridCellAdapterToDate(date, month, year);
		}
		if (v == nextMonth) {
			if (month > 11) {
				month = 1;
				year++;
			} else {
				month++;
			}
			setGridCellAdapterToDate(date, month, year);
		}
	}

	public String convertMonth(int month) {
		String monthAsString = "";
		if (month == 1) {
			monthAsString = "Tháng 1";
		}
		if (month == 2) {
			monthAsString = "Tháng 2";
		}
		if (month == 3) {
			monthAsString = "Tháng 3";
		}
		if (month == 4) {
			monthAsString = "Tháng 4";
		}
		if (month == 5) {
			monthAsString = "Tháng 5";
		}
		if (month == 6) {
			monthAsString = "Tháng 6";
		}
		if (month == 7) {
			monthAsString = "Tháng 7";
		}
		if (month == 8) {
			monthAsString = "Tháng 8";
		}
		if (month == 9) {
			monthAsString = "Tháng 9";
		}
		if (month == 10) {
			monthAsString = "Tháng 10";
		}
		if (month == 11) {
			monthAsString = "Tháng 11";
		}
		if (month == 12) {
			monthAsString = "Tháng 12";
		}

		return monthAsString;
	}

	@Override
	public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
		List<Prediction> predictions = new ArrayList<Prediction>();
		predictions = gestureLib.recognize(gesture);

		for (Prediction prediction : predictions) {
			if (prediction.score > 1.0) {
				if (prediction.name.equals("swipeLeft")) {
					if (month > 11) {
						month = 1;
						year++;
					} else {
						month++;
					}
					setGridCellAdapterToDate(date, month, year);
				}
				if (prediction.name.equals("swipeRight")) {
					if (month <= 1) {
						month = 12;
						year--;
					} else {
						month--;
					}
					setGridCellAdapterToDate(date, month, year);
				}
				// Toast.makeText(getApplicationContext(), prediction.name,
				// Toast.LENGTH_SHORT).show();
			}
		}

	}

}
